<?php

	include_once("./db.php"); 
	
	$userId = $_SESSION["user"]["Id"];
	
	$combinationId = $_REQUEST["combinationId"];
	$message = $_REQUEST["message"];
	$vote = $_REQUEST["vote"];
	$voteId = $_REQUEST["voteId"];
	
	if($vote == 1)
	{
		$ret = db_scalar("SELECT Id
			FROM Vote
			WHERE 
			AuthorId = $userId
			AND value = 1");
			
		if($ret)
		{
			$res = db_query("DELETE FROM Vote 
			WHERE Id = $ret AND AuthorId = $userId");
			
			$result["msg"] = "down";
			$result["sucess"] = "true";
			
			echo json_result_encode($result);
			
			return;
		}
	}
	
	
	$res["sucess"] = true;
	
	if($voteId)
	{
		$fields["VoteId"] = $voteId;
		
		if($message=="")
		{
			db_query("DELETE FROM Vote 
			WHERE Id = @voteId AND AuthorId = $userId", array("voteId" => $voteId));
			
			$res["delete"] = "delete";
			
			echo json_result_encode($res);
			
		}
	}
		
	$fields["VoteCombinationId"] = $combinationId;
	$fields["VoteValue"] = $vote;
	$fields["VoteMessage"] = $message;
	$fields["VoteAuthorId"] = $userId;

	$ret = db_insert("Vote", $fields);
	
	$res["Vote"] = $ret;
	
	
	
	echo json_result_encode($res);

?>



